//
//  NotesDetailCell.m
//  hai游～
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 jakf_17. All rights reserved.
//

#import "NotesDetailCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width

@implementation NotesDetailCell

- (void)dealloc
{
    
    [self.topDateLabel release];
    [self.bottomDateLabel release];
    [self.locationLabel release];
    [self.photo release];
    [self.notesLabel release];
    [self.cellView release];
    [self.photo_info release];
    [self.photoWeb release];
    [self.local_time release];
    [self.shadow release];
//    [self.dateLabel release];
    [self.poi release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.photo_info = [NSDictionary dictionary];
        self.poi = [NSDictionary dictionary];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        
        self.cellView = [[UIView alloc] initWithFrame:CGRectZero];
        // R 190 G 231 B 233
        //  9.R 236 G 173 B 158 6.R 214 G 213 B 183
        // 10.R 244 G 96 B 108
        self.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:199 / 255.0 blue:141 / 255.0 alpha:0.1];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_cellView];
        [_cellView release];
        
        self.photo = [[UIImageView alloc] initWithFrame:CGRectZero];
        _photo.backgroundColor = [UIColor clearColor];
        [self.cellView addSubview:_photo];
        [_photo release];
        
        self.locationView = [[UIButton alloc] initWithFrame:CGRectZero];
        _locationView.backgroundColor = [UIColor whiteColor];
        [self.cellView addSubview:_locationView];
        [_locationView release];

        
        self.locationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.locationView addSubview:self.locationLabel];
        [self.locationLabel release];
        
        self.locationImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.locationView addSubview:self.locationImage];
        [self.locationImage release];
        
        
        self.notesLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.notesLabel.font = [UIFont systemFontOfSize:12];
        _notesLabel.numberOfLines = 0;
        [self.cellView addSubview:self.notesLabel];
        [_notesLabel release];
        
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.dateLabel.font = [UIFont systemFontOfSize:12];
        [self.cellView addSubview:self.dateLabel];
        
        self.shadow = [[UILabel alloc] initWithFrame:CGRectZero];
        self.shadow.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1];
        [self.contentView addSubview:self.shadow];
        [self.shadow release];
        
        
    }
    return self;
}

// 自动布局cell子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"%s", __func__);
    self.contentView.backgroundColor = [UIColor clearColor];
#pragma mark 判断地点是否存在
    // [self.notes.photo isEqualToString:@""] || [self.notes.photo_info isKindOfClass:[NSNull class]]
    NSDictionary *photo_infoDic = self.photo_info;
    if ([self.photoWeb isEqualToString:@""] || photo_infoDic == nil ) {
        _imageHeight = 0;
        [self createNotesLabel];
        [self createLocationView];
        [self createCellView];
        [self judgeLocation];
        [self createDateLabel];
        self.cellView.backgroundColor = [UIColor whiteColor];
        self.photo.frame = CGRectZero;
    } else {
    // 获取图片大小, 根据label宽度 设置图片宽度
        [self createImageView];
        [self createNotesLabel];
        [self createCellView];
        [self judgeLocation];
        [self createDateLabel];
    
        NSLog(@"WIDTH === %f", WIDTH);
    }    
}
#pragma mark photo大小
- (void)createImageView
{
    NSNumber *num1 = [self.photo_info objectForKey:@"h"];
    NSLog(@"num1 ====== %@", num1);
    CGFloat height = [num1 floatValue];
    NSLog(@"height ====== %f", height);
    NSNumber *num2 = [self.photo_info objectForKey:@"w"];
    CGFloat width = [num2 floatValue];
    self.imageHeight = WIDTH * 340 / 375 * height / width;
    self.photo.frame = CGRectMake(0, 0, WIDTH * 345 / 375, _imageHeight);
    
}

- (void)createLocationView
{
#warning 点击事件未添加
    [self.locationLabel setText:self.locationStr];
    _locationLabel.font = [UIFont systemFontOfSize:12];
    NSLog(@"地点信息 %@" , _locationLabel.text);
    _locationLabel.textColor = [UIColor colorWithRed:25 / 255.0 green:202 / 255.0 blue:173 / 255.0 alpha:1.0];
    [self.locationView addSubview:self.locationLabel];
}

#pragma mark 判断地点信息
- (void)judgeLocation
{
    NSDictionary *dic = self.poi;
    if ([dic objectForKey:@"name"]) {
        self.locationStr = [self.poi objectForKey:@"name"];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        self.locationRect = [self.locationStr boundingRectWithSize:CGSizeMake(3000000, WIDTH * 15 / 375) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        _locationLabel.frame = CGRectMake(WIDTH * 25 / 375, 0, self.locationRect.size.width, WIDTH * 15 / 375);
        self.locationView.frame = CGRectMake(_cellView.frame.size.width - self.locationRect.size.width - WIDTH * 50 / 375  , _rect.size.height + _imageHeight + WIDTH * 20 / 375 , self.locationRect.size.width + WIDTH * 35 / 375, WIDTH * 15 / 375);
        self.locationImage.frame = CGRectMake(WIDTH * 10 / 375, 0, WIDTH * 15 / 375, WIDTH * 15 / 375);
        self.locationView.layer.cornerRadius = 7.5;
        [self.locationView.layer masksToBounds];
        NSURL *url = [NSURL URLWithString:@"http://media.breadtrip.com/images/icons/2/poi_category_11.png"];
        
        [self.locationImage sd_setImageWithURL:url];
        [self.locationView addSubview:self.locationImage];
        [self createLocationView];

    } else if ([self.country isEqualToString:@""] && [self.province isEqualToString:@""] && [self.city isEqualToString:@""] && ![dic objectForKey:@"name"]){
        self.locationStr = nil;
        self.locationRect = CGRectZero;
        _locationLabel.frame = CGRectZero;
        self.locationView.frame = CGRectZero;
        self.locationImage.image = nil;
        [self createLocationView];
    } else {
        self.locationStr = [NSString stringWithFormat:@"%@ %@ %@", self.country, self.province, self.city];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        self.locationRect = [self.locationStr boundingRectWithSize:CGSizeMake(3000000, WIDTH * 15 / 375) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//        
//        _locationLabel.frame = CGRectMake(WIDTH * 15 / 375, 0, self.locationRect.size.width, WIDTH * 15 / 375);
//        self.locationView.frame = CGRectMake(_cellView.frame.size.width - self.locationRect.size.width - WIDTH * 25 / 375 , _rect.size.height + _imageHeight + WIDTH * 25 / 375 , self.locationRect.size.width + WIDTH * 15 / 375, WIDTH * 15 / 375);
//        self.locationImage.frame = CGRectMake(0, 0, WIDTH * 15 / 375, WIDTH * 15 / 375);
        _locationLabel.frame = CGRectMake(WIDTH * 25 / 375, 0, self.locationRect.size.width, WIDTH * 15 / 375);
        self.locationView.frame = CGRectMake(_cellView.frame.size.width - self.locationRect.size.width - WIDTH * 50 / 375  , _rect.size.height + _imageHeight + WIDTH * 20 / 375 , self.locationRect.size.width + WIDTH * 35 / 375, WIDTH * 15 / 375);
        self.locationImage.frame = CGRectMake(WIDTH * 10 / 375, 0, WIDTH * 15 / 375, WIDTH * 15 / 375);
        self.locationView.layer.cornerRadius = 7.5;
        [self.locationView.layer masksToBounds];
        NSURL *url = [NSURL URLWithString:@"http://media.breadtrip.com/images/icons/2/poi_category_11.png"];
        
        [self.locationImage sd_setImageWithURL:url];
        [self.locationView addSubview:self.locationImage];
        [self createLocationView];
    }
}

- (void)createDateLabel
{
    NSString *str = self.local_time;
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(3000000, WIDTH * 15 / 375) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.dateLabel.frame = CGRectMake(WIDTH * 10 / 375, _rect.size.height + _imageHeight + WIDTH * 20 / 375, rect.size.width * 1, WIDTH * 15 / 375);
    self.dateLabel.textColor = [UIColor lightGrayColor];
    NSLog(@"rect.size.width = %f", rect.size.width);
    self.dateLabel.text = str;
    
}

- (void)createNotesLabel
{
    self.notesLabel.backgroundColor = [UIColor clearColor];
    self.notesLabel.textAlignment = NSTextAlignmentLeft;
    NSString *str = self.notesLabel.text;
    _notesLabel.numberOfLines = 0;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    self.rect = [str boundingRectWithSize:CGSizeMake(WIDTH * 325 / 375, 15000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.notesLabel.frame = CGRectMake(WIDTH * 10 / 375, _imageHeight + WIDTH * 10 / 375, WIDTH * 325 / 375, _rect.size.height);
    NSLog(@"游记正文高度 =========== %f", _rect.size.height);
}
#pragma mark view大小
- (void)createCellView
{
    self.cellView.frame = CGRectMake(WIDTH * 15 / 375, WIDTH * 15 / 375, WIDTH * 345 / 375 , _rect.size.height + WIDTH * 50 / 375 + _imageHeight);
    self.cellView.backgroundColor = [UIColor whiteColor];
    
    self.shadow.frame = CGRectMake(WIDTH * 15 / 375  + 2, WIDTH * 15 / 375 + _rect.size.height + WIDTH * 50 / 375 + _imageHeight, WIDTH * 345 / 375 - 1, 4);
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
